home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / dvivt.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  30KB  |  955 lines

  1. /* This file contains the output drivers for
  2.                 LaserJets, Epson, Vt100's and pc screens.
  3.         If some kind sole would like to add LA100 etc, go right ahead,
  4.                 (you will have to modify DVITOBIT.C to call your
  5.                 extra dev_size_la100, and dev_print_la100 routines)
  6.  
  7.   Thanks to     Evan Wilson, N Z Dairy Group, for adding the sixel driver
  8.                 (for la100's la75's etc.)
  9.  
  10. */
  11.  
  12. #if ( defined DJ || defined EMXOS2 )   /* a.r. */
  13. #define unix
  14. #endif
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include "int32.h"
  22.  
  23. #ifdef DJ /* a.r. if Hartmut releases GRX for Linux replace DJ -> LINUX */
  24. #  ifndef __linux__
  25. #    include <pc.h>
  26. #  endif
  27. #  include <grx.h>
  28. #  include <bccgrx.h>
  29. #endif
  30. #ifdef __TURBOC__
  31. #  include <conio.h>
  32. #  include <graphics.h>
  33. #endif
  34.  
  35. #include "bmp.h"
  36. #define false 0
  37. #define true !false
  38.  
  39. char *bgidir();
  40. int cmtobits(double *x, int *nx, double dpi);
  41. int printstrd(char *s, int i);
  42. int printstr(char *s);
  43. extern int dev_hpplus,dev_vhires,dev_hires,bmp_compress,dev_lowmem,dev_low,iscolor;
  44. extern int dev_noff,dev_nosquash,dev_wide,dev_noclip,dev_over,dev_noflip;
  45. extern char outbitname[];
  46.  
  47. #ifdef __TURBOC__
  48. dev_size_vt(int *nxbits, int *nybits, double *devxcm, double *devycm)
  49. {
  50.         *devxcm = 20;
  51.         *devycm = 15;
  52.         dev_noflip = true;
  53.         dev_hires = true;
  54.         if (dev_hires) {
  55.                 *nxbits = 400;
  56.                 *nybits = 400;
  57.         } else {
  58.                 *nxbits = 70;
  59.                 *nybits = 50;
  60.         }
  61. }
  62. dev_print_vt(Bitmap *b)
  63. {
  64.    int gdriver = DETECT, gmode, errorcode;
  65.    int x,y,z;
  66.    int white;
  67.    char *line;
  68.    initgraph(&gdriver, &gmode, bgidir());
  69.    errorcode = graphresult();
  70.    if (errorcode != grOk)  /* an error occurred */
  71.    {
  72.           printf("Graphics error: %s\n", grapherrormsg(errorcode));
  73.           printf("Press any key to halt:");
  74.           getch();
  75.           exit(1);             /* return with error code */
  76.    }
  77.  
  78.    white = getmaxcolor();
  79.         for (y=0; y<b->ny; y++) {
  80.           line = bmp_row(b,b->ny-y);
  81.           setfillstyle(SOLID_FILL,getmaxcolor());
  82.           for (x=0; x< b->nx ; x++) {
  83.                 z = 1 << (x % 8);
  84.                 if ((z & line[x/8]) != 0) {
  85.                   if (dev_hires) {
  86.                         putpixel(x,y,white);
  87.                   } else {
  88.                                 bar(x*10,y*10,x*10+8,y*10+8);
  89.                   }
  90.                 }
  91.           }
  92.         }
  93.  
  94.    /* clean up */
  95.    getch();
  96.    closegraph();
  97.    return 0;
  98. }
  99. #elif DJ  /* a.r. if Hartmut releases GRX for Linux replace DJ -> LINUX */
  100. dev_size_vt(int *nxbits, int *nybits, double *devxcm, double *devycm)
  101. {
  102.    int gdriver = DETECT, gmode, errorcode;
  103.    int maxx,maxy;
  104.  
  105.         *devxcm = 20;
  106.         *devycm = 15;
  107.         dev_noflip = true;
  108.         dev_hires = true;
  109.  
  110.    initgraph(&gdriver, &gmode, bgidir());
  111.    errorcode = graphresult();
  112.    if (errorcode != grOk) 
  113.    {
  114.           printf("Graphics error: %s\n", grapherrormsg(errorcode));
  115.           printf("Press any key to halt:");
  116.           getch();
  117.           exit(1);
  118.    }
  119.  
  120.  
  121.         if (dev_hires) {
  122.                 *nxbits = getmaxx() /*1000*/;
  123.                 *nybits = getmaxy() /*1000*/;
  124.         } else {
  125.                 *nxbits = 70;
  126.                 *nybits = 50;
  127.         }
  128.         closegraph();
  129. }
  130. dev_print_vt(Bitmap *b)
  131. {
  132.    int gdriver = DETECT, gmode, errorcode;
  133.    int x,y,z;
  134.    int white;
  135.    char *line;
  136.    initgraph(&gdriver, &gmode, bgidir());
  137.    errorcode = graphresult();
  138.    if (errorcode != grOk)  /* an error occurred */
  139.    {
  140.           printf("Graphics error: %s\n", grapherrormsg(errorcode));
  141.           printf("Press any key to halt:");
  142.           getch();
  143.           exit(1);             /* return with error code */
  144.    }
  145.  
  146.    white = 15 /*getmaxcolor()*/; /* a.r. 256 colors ... */
  147.         for (y=0; y<b->ny; y++) {
  148.           line = bmp_row(b,b->ny-y);
  149.           setfillstyle(SOLID_FILL,getmaxcolor());
  150.           for (x=0; x< b->nx ; x++) {
  151.                 z = 1 << (x % 8);
  152.                 if ((z & line[x/8]) != 0) {
  153.                   if (dev_hires) {
  154.                         putpixel(x,y,white);
  155.                   } else {
  156.                                 bar(x*10,y*10,x*10+8,y*10+8);
  157.                   }
  158.                 }
  159.           }
  160.         }
  161.  
  162.    /* clean up */
  163.    getch();
  164.    closegraph();
  165.    return 0;
  166. }
  167. #else
  168. dev_size_vt(int *nxbits, int *nybits, double *devxcm, double *devycm)
  169. {
  170.         *devxcm = 20;
  171.         *devycm = 15;
  172.         *nxbits = 76;
  173.         *nybits = 22;
  174. }
  175. dev_print_vt(Bitmap *b)
  176. {
  177.    int x,y,z;
  178.    char *line;
  179.  
  180.         for (y=0; y<b->ny; y++) {
  181.           line = bmp_row(b,b->ny-y);
  182.           for (x=0; x<b->nx ; x++) {
  183.                 z = 1 << (x % 8);
  184.                 if ((z & line[x/8]) != 0) {
  185.                         printf("#");
  186.                 } else printf(" ");
  187.           }
  188.           printf("[%d]\n",b->ny-y);
  189.         }
  190. }
  191. #endif
  192.  
  193. dev_print_sx(Bitmap *b)
  194. {
  195.                 static unsigned char *out_buff;
  196.                 unsigned char *line;
  197.                 int nny, x, y, ly, r, z, nout;
  198.                 char *c, *o, buff[8];
  199.  
  200.                 out_buff = (ucharp) malloc(b->nx);
  201.                 if (out_buff==NULL) {
  202.                                 printf("Memory allocation failure for outbuff \n");
  203.                                 exit(1);
  204.                 }
  205.                 if (dev_vhires) {
  206.                         printstr ("\x1bP;;2q\"1;1");        /* Graphics mode very high res /
  207.                 } else if (dev_hires) {
  208.                         printstr ("\x1bP;;5q\"1;1");        /* Graphics mode high res */
  209.                 } else {
  210.                         printstr ("\x1bP;;10q\"1;1");       /* Graphics mode low res */
  211.                 }
  212.                 nny = ((b->ny-1)/6+1)*6;
  213.                 for (ly=nny-6; ly>=0; ly-=6) {          /* Step down 6 rows at a time */
  214.                         for (x=0; x<b->nx; x++) out_buff[x] = 0;    /* Zero output array */
  215.                         for (r=0,y=ly; r<6; y++,r++) {      /* Step up 1 row at a time */
  216.                                 if (y<b->ny) {
  217.                                         line = (ucharp) bmp_row(b,y);
  218.                                         for (x=0; x<b->nx; x++) {
  219.                                                 z = 1 << (x % 8);
  220.                                                 if ((z & line[x/8]) == 0) {
  221.                                                         out_buff[x] |= 1 << (5 - r);
  222.                                                 }
  223.                                         }
  224.                                 }
  225.                         }
  226.                         for (nout=(b->nx); out_buff[nout-1]==0 && nout>0; nout--);  /* Remo/
  227.                         if (nout > 0) {
  228.                                 c = &out_buff[0];
  229.                                 o = &out_buff[0];
  230.                                 for (x=0; x<nout; x++) {                /* Step through the lin/
  231.                                         out_buff[x] += 0x3f;                /* Add sixel format off/
  232.                                         if (out_buff[x] != *c) {
  233.                                                 if (&out_buff[x]-c > 5) {   /* > 5 repeats, output comp/
  234.                                                         sprintf (buff, "!%d%c", &out_buff[x]-c, *c);
  235.                                                         for (r=0; buff[r]!=0; r++) *o++ = buff[r];
  236.                                                 } else {
  237.                                                         for (r=0; r<&out_buff[x]-c; r++) *o++ = *c;
  238.                                                 }
  239.                                                 c = &out_buff[x];
  240.                                         }
  241.                                 }
  242.                                 for (r=0; r<=&out_buff[nout]-c; r++) *o++ = *c;
  243.                                 nout = o - out_buff;
  244.                                 printmem (out_buff, nout);
  245.                         }
  246.                         printstr ("-");                     /* Graphics new line */
  247.                 }
  248.                 printstr ("\x1b\\\n");                  /* Graphics mode terminator */
  249. #if ( defined __TURBOC__ || defined DJ || defined EMXOS2 )
  250.                 /* vax print symbiont adds a FF character itself */
  251.                 if (!dev_noff) printstr("\x0c"); /* form feed, reset origin */
  252. #endif
  253. }
  254.  
  255.  
  256.  
  257. dev_size_sx(int *nxbits, int *nybits, double *devxcm, double *devycm)
  258. {
  259.                 if (dev_wide) {
  260.                                 if (!dev_nosquash && *devxcm>33) *devxcm = 33;
  261.                 } else {
  262.                                 if (!dev_nosquash && *devycm>29) *devycm = 29;
  263.                                 if (!dev_nosquash && *devxcm>20) *devxcm = 20;
  264.                 }
  265.                 if (dev_vhires) {
  266.                                 cmtobits(devxcm,nxbits,360);
  267.                                 cmtobits(devycm,nybits,360);
  268.                 } else if (dev_hires) {
  269.                                 cmtobits(devxcm,nxbits,144);
  270.                                 cmtobits(devycm,nybits,144);
  271.                 } else {
  272.                                 cmtobits(devxcm,nxbits,72);
  273.                                 cmtobits(devycm,nybits,72);
  274.                 }
  275. }
  276.  
  277.  
  278. dev_size_lj(int *nxbits, int *nybits, double *devxcm, double *devycm)
  279. {
  280.         if (dev_wide) {
  281.                 if (!dev_nosquash && *devxcm>28) *devxcm = 28;
  282.         } else {
  283.                 if (!dev_nosquash && *devycm>27) *devycm = 27;
  284.                 if (!dev_nosquash && *devxcm>19) *devxcm = 19;
  285.         }
  286.         if (dev_lowmem) {
  287.                 *devxcm = 16;
  288.                 *devycm = 12;
  289.         }
  290.         if (dev_hires) {
  291.                 cmtobits(devxcm,nxbits,300);
  292.                 cmtobits(devycm,nybits,300);
  293.         } else {
  294.                 cmtobits(devxcm,nxbits,150);
  295.                 cmtobits(devycm,nybits,150);
  296.         }
  297. }
  298.  
  299. int ljsendline(char *s, int nc, int y);
  300. dev_print_lj(Bitmap *b)
  301. {
  302.         int x,y,nout;
  303.         static unsigned char *out_buff;
  304.         char pbuff[80];
  305.         unsigned char swapbit[257];
  306.         unsigned char swapnib[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
  307.         unsigned char *o,c,*line;
  308.         int nc,i,n1,n2;
  309.  
  310.         out_buff = (ucharp) malloc(b->nx + 10);
  311.         if (out_buff==NULL) {
  312.                 printf("Memory allocation failure for outbuff \n");
  313.                 exit(1);
  314.         }
  315.         for (i=0;i<256;i++) {
  316.                 n1 = i & 0xf;
  317.                 n2 = (i >> 4) & 0xf;
  318.                 swapbit[i] = swapnib[n2] | (swapnib[n1] << 4);
  319.         }
  320.         for (i=0;i<256;i++) swapbit[i] = 255-swapbit[i]; /* invert */
  321.  
  322.         if (!dev_noff)  printstrd("%cE",27);    /* reset  */
  323.         if (dev_hires) printstr("\x1b*t300R");  /* set resolution */
  324.         else           printstr("\x1b*t150R");
  325.         if (dev_hpplus)  printstr("\x1b*b1M");  /* compact */
  326.         printstr("\x1b*r1A");           /* start graphics at cur pos */
  327.         for (y=(b->ny-1); y>=0; y--) {
  328.           line = (ucharp) bmp_row(b,y);
  329.           for (nout=(b->nx/8+1); line[nout-1]==255 && nout>0; nout--);
  330.           o = &out_buff[0];
  331.           for (x=0; x<nout; x++) {
  332.                   c = line[x];
  333.                   nc = 0;
  334.                   if (dev_hpplus) {
  335.                   for (;x<nout-1 && line[x+1]==c && nc<250; x++) nc++;
  336.                   *o++ = nc;
  337.                   }
  338.                   *o++ = swapbit[c];
  339.           }
  340.           ljsendline((char *) out_buff,o-out_buff,y);
  341.         }
  342.         printstr("\x1b*rB");        /* end graphics */
  343. #if ( defined __TURBOC__ || defined DJ || defined EMXOS2 )
  344.         /* vax print symbiont adds a FF character itself */
  345.         if (!dev_noff) printstr("\x0c"); /* form feed, reset origin */
  346. #endif
  347. }
  348. ljsendline(char *s, int nc,int y)
  349. {
  350.         printstrd("\x1b*b%dW",nc);  /* send nc bytes */
  351.         printmem(s,nc);
  352. }
  353. printstrd(char *s, int i)
  354. {
  355.         static char pbuff[128];
  356.         sprintf(pbuff,s,i);
  357.         printmem(pbuff,strlen(pbuff));
  358. }
  359. printstr(char *s)
  360. {
  361.         printmem(s,strlen(s));
  362. }
  363.  
  364. dev_size_pj(int *nxbits, int *nybits, double *devxcm, double *devycm)
  365. {
  366.         if (dev_wide) {
  367.                 if (!dev_nosquash && *devxcm>28) *devxcm = 28;
  368.         } else {
  369.                 if (!dev_nosquash && *devycm>27) *devycm = 27;
  370.                 if (!dev_nosquash && *devxcm>19) *devxcm = 19;
  371.         }
  372.         iscolor = true;
  373.         if (dev_hires) {
  374.                 cmtobits(devxcm,nxbits,180);
  375.                 cmtobits(devycm,nybits,180);
  376.         } else {
  377.                 cmtobits(devxcm,nxbits,90);
  378.                 cmtobits(devycm,nybits,90);
  379.         }
  380. }
  381. cmtobits(double *x, int *nx, double dpi)
  382. {
  383.         *nx = (int) dpi*(*x / 2.54);
  384.         *nx = *nx/8;
  385.         *nx = *nx*8+8;
  386.            (*nx)--;
  387.         *x = (*nx/dpi)*2.54;
  388. }
  389. int pjsendline(char *s, int nc, int y, int plane);
  390. dev_print_pj(Bitmap *b)
  391. {
  392.         int x,y,nout;
  393.         static unsigned char *out_buff, *out_buff2;
  394.         static unsigned char *out_buffp0, *out_buffp1, *out_buffp2;
  395.         char pbuff[80];
  396.         unsigned char swapbit[257];
  397.         unsigned char swapnib[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
  398.         unsigned char *o,c,*line;
  399.         int nc,i,n1,n2,plane;
  400.  
  401.         out_buff = (ucharp) malloc(b->nx + 10);
  402.         out_buffp0 = (ucharp)  malloc(b->nx + 10);
  403.         out_buffp1 = (ucharp) malloc(b->nx + 10);
  404.         out_buffp2 = (ucharp) malloc(b->nx + 10);
  405.         if (out_buff==NULL || out_buffp2==NULL) {
  406.                 printf("Memory allocation failure for outbuff \n");
  407.                 exit(1);
  408.         }
  409.         for (i=0;i<256;i++) {
  410.                 n1 = i & 0xf;
  411.                 n2 = (i >> 4) & 0xf;
  412.                 swapbit[i] = swapnib[n2] | (swapnib[n1] << 4);
  413.         }
  414. /*  for (i=0;i<256;i++) swapbit[i] = 255-swapbit[i];  invert */
  415.  
  416.  
  417.  
  418.         if (!dev_noff)  printstrd("%cE",27);    /* reset  */
  419.         if (dev_over) printstrd("%c&k3W",27); /* transparency mode */
  420.         if (dev_hires) printstr("\x1b*t180R");  /* set resolution */
  421.         else           printstr("\x1b*t90R");
  422.         if (dev_hpplus)  printstr("\x1b*b1M");  /* compact */
  423.  
  424.         printstr("\x1b*r3U");   /* use 3 color planes, 8 colors*/
  425.         printstrd("\x1b*r%dS",b->nx); /* set width of image */
  426.         printstr("\x1b*r1A");           /* start graphics at cur pos */
  427.  
  428.  
  429.         for (y=(b->ny-1); y>=0; y--) {
  430.           line = (ucharp) bmp_row(b,y);
  431.           nout = b->nx/8;
  432.           for (x=0; x<=b->nx/8; x++) {  /* extract one plane of bits */
  433.                   for (plane = 0; plane < 3; plane++) {
  434.                 c = bitplane(line,x,plane);
  435.                 switch (plane) {
  436.                   case 0:
  437.                         out_buffp0[x] = c; break;
  438.                   case 1:
  439.                         out_buffp1[x] = c; break;
  440.                   case 2:
  441.                         out_buffp2[x] = c; break;
  442.                 }
  443.                   }
  444.           }
  445.  
  446.           for (plane = 0; plane < 3; plane++) {
  447.                 switch (plane) {
  448.                   case 0:
  449.                 out_buff2 = out_buffp0;
  450.                 break;
  451.                   case 1:
  452.                 out_buff2 = out_buffp1;
  453.                 break;
  454.                   case 2:
  455.                 out_buff2 = out_buffp2;
  456.                 break;
  457.                 }
  458.                 o = &out_buff[0];
  459.                 for (x=0; x<=nout; x++) {
  460.                   c = out_buff2[x];
  461.                   nc = 0;
  462.                   if (dev_hpplus) {  /* compress if requested */
  463.                   for (;x<nout-1 && out_buff2[x+1]==c && nc<250; x++) nc++;
  464.                   *o++ = nc;
  465.                   }
  466.                   *o++ = swapbit[c];
  467.                 }
  468.  
  469.                 pjsendline((char *) out_buff,o-out_buff,y,plane);
  470.           }
  471.         }
  472.         printstr("\x1b*rB");        /* end graphics */
  473. #if ( defined __TURBOC__ || defined DJ || defined EMXOS2 )
  474.         /* vax print symbiont adds a FF character itself */
  475.         if (!dev_noff) printstr("\x0c"); /* form feed, reset origin */
  476. #endif
  477. }
  478. int pjsendline(char *s, int nc,int y,int plane)
  479. {
  480.         int x;
  481.  
  482.         if (plane==2)   printstrd("\x1b*b%dW",nc);  /* send nc bytes */
  483.         else        printstrd("\x1b*b%dV",nc);  /* send nc bytes */
  484.         printmem(s,nc);
  485. }
  486.  
  487. bitplane(char line[],int x, int p)
  488. {
  489.         /* x is byte count */
  490.         /* construct a byte from line[x].hi.low.p  ... line[x+7] */
  491.         register xx,i,curbit,c,pm;
  492.         c = 0;
  493.         x = x*8;
  494.         pm = 1 << p;
  495.         curbit = 1;
  496.         for (xx=x; xx<x+8; xx++) {
  497.                 i = line[xx/2];
  498.                 if (xx % 2==0) i = (i & 0xf0)>>4;
  499.                 else i = i & 0xf;
  500.                 i = i & pm;
  501.                 if (i!=0) c = c + curbit;
  502.                 curbit = curbit << 1;
  503.         }
  504.         return c;
  505. }
  506.  
  507.  
  508. dev_size_ep(int *nxbits, int *nybits, double *devxcm, double *devycm)
  509. {
  510.         if (dev_wide) {
  511.                 if (!dev_nosquash && *devxcm>28) *devxcm = 28;
  512.         } else {
  513.                 if (!dev_nosquash && *devycm>27) *devycm = 27;
  514.                 if (!dev_nosquash && *devxcm>19) *devxcm = 19;
  515.         }
  516.         if (dev_hires) {
  517.                 cmtobits(devxcm,nxbits,180);
  518.                 cmtobits(devycm,nybits,180);
  519.         } else {
  520.                 cmtobits(devxcm,nxbits,120);
  521.                 cmtobits(devycm,nybits,72);
  522.         }
  523. }
  524.  
  525. dev_print_ep(Bitmap *b)
  526. {
  527.         int nny,x,y,nout;
  528.         static unsigned char *out_buff;
  529.         char pbuff[80];
  530.         unsigned char *o,c,*line;
  531.         int nc,i,n1,n2,r,z;
  532.  
  533.         if (dev_hires) {
  534.                 dev_print_ep24(b);
  535.                 return;
  536.         }
  537.         out_buff = (ucharp) malloc(b->nx + 10);
  538.         if (out_buff==NULL) {
  539.                 printf("Memory allocation failure for outbuff \n");
  540.                 exit(1);
  541.         }
  542.         nny = b->ny;
  543.         if (8*(nny/8) != nny) nny = (1+(nny/8))*8;
  544.         for (y=nny-8 ; y>=0 ; y-=16) {
  545.                 for (r=0;r<(b->nx+5);r++) out_buff[r] = 0; /* zero array */
  546.                 for (r=0; r<8 ; r++,y++) {  /* for each of 8 columns */
  547.                         line = (ucharp) bmp_row(b,y);
  548.                         if (b->ny>=y) {
  549.                         for (x=0; x<b->nx;x++) {
  550.                                 z = 1 << (x % 8);
  551.                                 if ((z & line[x/8]) == 0) {
  552.                                          out_buff[x] |= 1 << r;
  553.                                 }
  554.                         }}
  555.                 }
  556.                 for (nout=(b->nx); out_buff[nout-1]==0 && nout>0; nout--);
  557.                 epsendline(out_buff,nout);
  558.         }
  559. #if ( defined __TURBOC__ || defined DJ || defined EMXOS2 )
  560.         /* vax print symbiont adds a FF character itself */
  561.         if (!dev_noff) printstr("\x0c"); /* form feed, reset origin */
  562. #endif
  563. }
  564. epsendline(char *s, int nc)
  565. {
  566.         char buff[20];
  567.         strcpy(buff,"\x1bL");
  568.         buff[2] = (nc) & 255;
  569.         buff[3] = (nc) / 256;
  570.         printmem(buff,4);
  571.         printmem(s,nc);
  572.         printstr("\x0d\x1bJ\x18");
  573. }
  574.  
  575. ep24sendline(char *s, int nc)
  576. {
  577.         char buff[20];
  578.         strcpy(buff,"\x1b*\x27");
  579.         nc = nc / 3 + 1;
  580.         buff[3] = (nc) & 255;
  581.         buff[4] = (nc) / 256;
  582.         printmem(buff,5);
  583.         printmem(s,nc*3);
  584.         printstr("\x0d\x1bJ\x18");
  585. }
  586. dev_print_ep24(Bitmap *b)
  587. {
  588.         int x,y,nout;
  589.         static unsigned char *out_buff;
  590.         char pbuff[80];
  591.         unsigned char *o,c,*line;
  592.         int nc,i,n1,n2,r,z,nny;
  593.  
  594.         out_buff = (ucharp) malloc(3*b->nx + 20);
  595.         if (out_buff==NULL) {
  596.                 printf("Memory allocation failure for outbuff \n");
  597.                 exit(1);
  598.         }
  599.         nny = b->ny;
  600.         if (24*(nny/24) != nny) nny = (1+(nny/24))*24;
  601.         for (y=nny-24 ; y>=0 ; y-=48) {
  602.                 /* printf("y=%d\n",y); */
  603.                 for (r=0;r<(b->nx*3+5);r++) out_buff[r] = 0; /* zero array */
  604.                 for (r=0; r<24; r++,y++) {  /* for each of 8 columns */
  605.                         line = (ucharp) bmp_row(b,y);
  606.                         if (b->ny>=y) {
  607.                         for (x=0; x<b->nx; x++) {
  608.                                 z = 1 << (x % 8);
  609.                                 if ((z & line[x/8]) == 0) {
  610.                                         if (r<8) {
  611.                                                  out_buff[x*3+2] |= 1 << r;
  612.                                         } else if (r<16) {
  613.                                                  out_buff[x*3+1] |= 1 << (r-8);
  614.                                         } else {
  615.                                                  out_buff[x*3+0] |= 1 << (r-16);
  616.                                         }
  617.                                 }
  618.                         }}
  619.                 }
  620.                 for (nout=(3*b->nx); out_buff[nout-1]==0 && nout>0; nout--);
  621.                 ep24sendline((char *)out_buff,nout);
  622.         }
  623. #if ( defined __TURBOC__ || defined DJ || defined EMXOS2 )
  624.         /* vax print symbiont adds a FF character itself */
  625.         if (!dev_noff) printstr("\x0c"); /* form feed, reset origin */
  626. #endif
  627. }
  628.  
  629. /* Word Perfect, tiff generation */
  630.  
  631. extern int fittobit;
  632. dev_size_wp(int *nxbits, int *nybits, double *devxcm, double *devycm)
  633. {
  634.         *devxcm = 24;
  635.         *devycm = 24;
  636.         *nxbits = 200;
  637.         *nybits = 200;
  638.         fittobit = true;
  639. }
  640.  
  641. #if ( defined DJ || defined EMXOS2 || defined EMXOS2 )   /* a.r. */
  642.  #undef unix
  643. #endif
  644.  
  645. #ifndef unix
  646. /* tiff stuff starts here */
  647.  
  648. int32 sa(int32 a);
  649. int32 ss(char *s);
  650. int32 sw(int x);
  651. int32 sl(int32 x);
  652. int32 read_eps(char *fname,FILE *tf);
  653. int sendmem(char *s, int32 j);
  654. int setl(int32 p, int32 x);
  655. int setw(int32 p, int x);
  656.  
  657. int32 tcnt; /* current byte position */
  658. extern FILE *outbit;
  659. FILE *tf;
  660. int addeps=true;
  661. int32 tstart;
  662.  
  663. dev_print_wp(Bitmap *b)
  664. {
  665.         /* 1=byte, 2=ascii, 3=short, 4=int32, 5=rational */
  666.         char fname[80];
  667.         int32 strip1,entrycount,stripoffsets,stripbcounts,ifd;
  668.         int32 datep,datelen,namep,namelen,i,j;
  669.         int32 h_ps,h_pslen,h_tiff,h_tifflen;
  670.  
  671.         tf = outbit; /* output file handle */
  672.  
  673.         if (addeps) {
  674.                 sw(0xd0c5); sw(0xc6d3);
  675.                 h_ps = sl(0);
  676.                 h_pslen = sl(0);
  677.                 sl(0); sl(0);
  678.                 h_tiff = sl(0);
  679.                 h_tifflen = sl(0);
  680.                 sw(0xffff);
  681.  
  682.                 setl(h_ps,sa(tcnt));
  683.                 i = tcnt;
  684.                 tcnt += read_eps(outbitname,tf);
  685.                 setl(h_pslen,tcnt-i);
  686.         }
  687.  
  688.  
  689.         tstart = sw(0x4949); /* byte order  4d 4d */
  690.         sw(0x002a);
  691.         ifd = sl(0x0000);
  692.         entrycount = sw(0);
  693.         setl(ifd,sa(entrycount));
  694.         sw(0xff); sw(3); sl(1); sw(1);  sw(0);      /* subfiletype */
  695.         sw(0x100); sw(3); sl(1); sl(b->nx);     /* image width */
  696.         sw(0x101); sw(3); sl(1); sl(b->ny);         /* image length */
  697.         sw(0x102); sw(3); sl(1); sw(1); sw(0);  /* bits per sample */
  698.         sw(0x103); sw(3); sl(1); sw(1); sw(0);  /* no compression */
  699.         sw(0x106); sw(3); sl(1); sw(1); sw(0);  /* black and white */
  700.         sw(0x111); sw(4); sl(1); stripoffsets = sl(0); /* offsets to image data */
  701.         sw(0x11c); sw(3); sl(1); sw(1); sw(0); /* planar config */
  702.         sw(0x131); sw(2); namelen = sl(1); namep = sl(0); /* Name  */
  703.         sl(0); /* end of table  */
  704.         setw(entrycount,9);
  705.         strip1 = tcnt;
  706.         send_image(b);
  707.         setl(stripoffsets,sa(strip1));
  708.  
  709.         namelen = strlen("GLE Tiff output.");
  710.         setl(namep,sa(ss("GLE Tiff output.")));
  711.  
  712.         if (addeps) {
  713.                 setl(h_tiff,tstart);
  714.                 setl(h_tifflen,tcnt-tstart);
  715.         }
  716.  
  717.         fclose(tf);
  718.  
  719. }
  720. send_image(Bitmap *b)
  721. {
  722.         int y;
  723.         unsigned char swapnib[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
  724.         unsigned char swapbit[257];
  725.         unsigned char savechar,temp;
  726.         static unsigned char *out_buff;
  727.         unsigned int r,i,j,newy,x,z;
  728.         unsigned char *line;
  729.         unsigned char *o,c;
  730.         int n1,n2;
  731.  
  732.         if (out_buff==0) out_buff = (ucharp) malloc(b->nx + 10);
  733.         if (out_buff==NULL) {
  734.                 printf("Memory allocation failure for outbuff \n");
  735.                 exit(1);
  736.         }
  737.         for (i=0;i<256;i++) {
  738.                 n1 = i & 0xf;
  739.                 n2 = (i >> 4) & 0xf;
  740.                 swapbit[i] = swapnib[n2] | (swapnib[n1] << 4);
  741.         }
  742.  
  743.         for (y=b->ny-1 ; y>=0 ; y-=1) {
  744.                 out_buff[0] = 0;
  745.                 line = (ucharp) bmp_row(b,y);
  746.                 for (i=0; i<b->nx/8; i++) {
  747.                         out_buff[i] = swapbit[line[i]];
  748.                 }
  749.  
  750.                 sendmem((char *) out_buff,b->nx/8);
  751.         }
  752. }
  753.  
  754.  
  755. int32 sa(int32 a)
  756. {
  757.         return a-tstart;
  758. }
  759. int32 sw(int x)
  760. {
  761.         fwrite(&x,2,1,tf);
  762.         tcnt += 2;
  763.         return tcnt-2;
  764. }
  765. int32 ss(char *s)
  766. {
  767.         int i;
  768.  
  769.         i = strlen(s)+1;
  770.         if (i&1 == 1) i++;
  771.         fwrite(s,1,i,tf);
  772.         tcnt += i;
  773.         return tcnt-i;
  774. }
  775. sendmem(char *s, int32 j)
  776. {
  777.         fwrite(s,1,j,tf);
  778.         tcnt += j;
  779. }
  780. int32 sl(int32 x)
  781. {
  782.         fwrite(&x,4,1,tf);
  783.         tcnt += 4;
  784.         return tcnt-4;
  785. }
  786. setl(int32 p, int32 x)
  787. {
  788.         if (fseek(tf,p,SEEK_SET)!=0)  printf("Seekl failed, %ld %ld\n",p,tcnt);
  789.         fwrite(&x,4,1,tf);
  790.         if (fseek(tf,tcnt,SEEK_SET)!=0) printf("Seekl eof failed, %ld \n",p);
  791. }
  792. setw(int32 p, int x)
  793. {
  794.         if (fseek(tf,p,SEEK_SET)!=0) printf("Seekw failed, %ld  %ld\n",p,tcnt);
  795.         fwrite(&x,2,1,tf);
  796.         if (fseek(tf,tcnt,SEEK_SET)!=0) printf("Seekw eof failed, %ld \n",p);
  797. }
  798.  
  799. int32 read_eps(char *fname,FILE *tf)
  800. {
  801.         FILE *eps;
  802.         static char buff[1002];
  803.         char *s;
  804.         int32 tot=0;
  805.         int n;
  806.  
  807.         strcpy(buff,fname);
  808.         s = strchr(buff,'.');
  809.         if (s!=NULL) *s = 0;
  810.         strcat(buff,".eps");
  811.  
  812.         eps = fopen(buff,"rb");
  813.         if (eps==NULL) {
  814.                 printf("Unable to open {%s}, you must PSGLE /EPS first\n",buff);
  815.                 abort();
  816.         }
  817.  
  818.         for (;!feof(eps);) {
  819.           n = fread(buff,1,1000,eps);
  820.           if (n>0) {
  821.                 fwrite(buff,1,n,tf);
  822.                 tot += n;
  823.           }
  824.         }
  825.         buff[0] = 13; buff[1] = 10; buff[2] = 0;
  826.         if (tot&1 == 1) {fwrite(" ",1,1,tf); tot++;}
  827.         fwrite(buff,1,2,tf); tot+=2;
  828.         return tot;
  829. }
  830. #else
  831. dev_print_wp(Bitmap *b){}
  832. #endif
  833.  
  834.  
  835.  
  836. dev_size_ec(int *nxbits, int *nybits, double *devxcm, double *devycm)
  837. {
  838.         if (dev_wide) {
  839.                 if (!dev_nosquash && *devxcm>28) *devxcm = 28;
  840.         } else {
  841.                 if (!dev_nosquash && *devycm>27) *devycm = 27;
  842.                 if (!dev_nosquash && *devxcm>19) *devxcm = 19;
  843.         }
  844.         iscolor = true;
  845.         if (dev_hires) {
  846.                 cmtobits(devxcm,nxbits,180);
  847.                 cmtobits(devycm,nybits,180);
  848.         } else {
  849.                 cmtobits(devxcm,nxbits,120);
  850.                 cmtobits(devycm,nybits,72);
  851.         }
  852.         (*nxbits)++;
  853.         (*nybits)++;
  854.         printf("size %d %d \n",*nxbits,*nybits);
  855. }
  856. int epsendcol(char *s, int nc, int col);
  857. int onecolor(char line[],char out[], int col, int nx);
  858.  
  859. static int y_line;
  860. dev_print_ec(Bitmap *b)
  861. {
  862.         int x,y,nout;
  863.         static unsigned char *outbuff;
  864.         char pbuff[80];
  865.         unsigned char *o,c,*line,*zline;
  866.         int k,nc,i,n1,n2,plane,thiscol;
  867.         int nny,r,z,zzy;
  868.  
  869.         outbuff = (ucharp) malloc(b->nx + 15);
  870.         line = (ucharp) malloc(b->nx + 15);
  871.         if (outbuff==NULL || line==NULL) {
  872.                 printf("Memory allocation failure for outbuff \n");
  873.                 exit(1);
  874.         }
  875.  
  876.  
  877.         nny = b->ny;
  878.         if (8*(nny/8) != nny) nny = (1+(nny/8))*8;
  879.         for (y=nny-8 ; y>=0 ; y-=16) {
  880.                 zzy = y;
  881.                 for (thiscol = 0; thiscol<9; thiscol ++) {
  882.                         y = zzy;
  883.                         for (r=0;r<(b->nx+10);r++) outbuff[r] = 0; /* zero array */
  884.                         for (r=0; r<8 ; r++,y++) {  /* for each of 8 rows (pins)  */
  885.                                 zline = (ucharp) bmp_row(b,y);
  886.                                 if (y<=b->ny) {
  887.                                  if (onecolor(zline,line,thiscol,b->nx)) {
  888.                                         for (x=0; x<b->nx;x++) {
  889.                                                 z = 1 << (x % 8);
  890.                                                 if ((z & line[x/8]) != 0) {
  891.                                                         outbuff[x] |= 1 << r;
  892.                                                 }
  893.                                         }
  894.                                  }
  895.                                 }
  896.                         }
  897.                 y_line = y;
  898.                         epsendcol(outbuff,b->nx,thiscol);
  899.                 }
  900.                 printstr("\x0d\x1bJ\x18");
  901.         }
  902. #if ( defined __TURBOC__ || defined DJ || defined EMXOS2 )
  903.         /* vax print symbiont adds a FF character itself */
  904.         if (!dev_noff) printstr("\x0c"); /* form feed, reset origin */
  905. #endif
  906. }
  907. int ecol[]={0,1,6,4,2,3,5,0,0,0,0,0,0,0,0,0,0,0,0};
  908. int epsendcol(char *s, int nc, int col)
  909. {
  910.         char buff[20];
  911.         int x;
  912.  
  913.         for (; s[nc-1]==0 && nc>0; nc--);   /* Don't send trailing blanks */
  914.         if (nc==0) return;
  915.         printf("(%d)",y_line);
  916.  
  917.         sprintf(buff,"\x1br%d\x1bL",ecol[col]);
  918.         buff[5] = (nc) & 255;
  919.         buff[6] = (nc) / 256;
  920.         printmem(buff,7);
  921.         printmem(s,nc);
  922.         printstr("\x0d");
  923. }
  924.  
  925.  
  926. int onecolor(char line[],char *out, int col, int nx)
  927. {
  928.         /* Extract a line of color=col, if blank then return false */
  929.         register b,xx,r,c;
  930.         char *outbf=out;
  931.         int i,curbit;
  932.         int nb = nx/8;
  933.  
  934.         r = false;
  935.  
  936.         for (i=0; i<nb; i++) {
  937.                 curbit = 1;
  938.                 c = 0;
  939.                 for (xx=i*8; xx<(i*8+8); xx++) {
  940.                         b = line[xx/2];
  941.                         if (xx % 2 == 0) b = (b & 0xf0)>>4;
  942.                         else b = b & 0xf;
  943.                         if (b==col) {
  944.                                 r = true;
  945.                                 c = c + curbit;
  946.                         }
  947.                         curbit = curbit << 1;
  948.                 }
  949.                 *outbf = c; outbf++;
  950.         }
  951.         return r;
  952. }
  953.  
  954.  
  955.